﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Audio;

namespace Bejeweled.Support
{

    public class Animation
    {
        public int numColumn;
        float nextFrameDelay;
        public int frameWidth;
        public int frameHeight;
        public float secondPerFrame;
        public int index;
        public Rectangle rect;
        public Animation(int _frameWidth, int _frameHeight, int _numColumn, float _secondPerFrame)
        {
            numColumn = _numColumn;
            secondPerFrame = _secondPerFrame;

            frameWidth = _frameWidth;
            frameHeight = _frameHeight;

            index = 0;
            //rect            =new Rect(0, frameWidth, 0, frameHeight);
            rect = new Rectangle(0, 0, frameWidth, frameHeight);
        }
        public Animation(Texture2D texture, int _numRow, int _numColumn, float _secondPerFrame)
        {
            numColumn = _numColumn;
            secondPerFrame = _secondPerFrame;

            frameWidth = (int)(texture.Width / _numColumn);
            frameHeight = (int)(texture.Height / _numRow);

            index = 0;
            rect = new Rectangle(0, 0, frameWidth, frameHeight);
        }
        public void ResetRect()
        {
            rect = new Rectangle((index % numColumn) * frameWidth, (index / numColumn) * frameHeight, frameWidth, frameHeight);
            //rect.X = (index % numColumn) * frameWidth;
            //rect.Y = (index / numColumn) * frameHeight;
        }
        public void SetIndex(int _index)
        {
            index = _index;
            ResetRect();
        }
        public Rectangle GetRect(int _index)
        {
            Rectangle result = new Rectangle();
            result.X = (_index % numColumn) * frameWidth;
            result.Y = (_index / numColumn) * frameHeight;
            return result;
        }
        /*public Rectangle GetRect(int beginIndex, int endIndex)
        {
            Rect r1=GetRect(beginIndex);
            Rect r2=GetRect(endIndex);
            int left=Math.min(r1.left, r2.left);
            int right=Math.max(r1.right, r2.right);
            int top=Math.min(r1.top, r2.top);
            int bottom=Math.max(r1.bottom, r2.bottom);
            return new Rect(left, right, top, bottom);
        }*/
        public void Update(int beginIndex, int endIndex, float t)
        {
            if (beginIndex == endIndex)
                SetIndex(beginIndex);
            else
            {
                if (beginIndex < 0)
                    return;
                if (index < beginIndex || index > endIndex)
                    SetIndex(beginIndex);
                nextFrameDelay += t;
                if (nextFrameDelay > secondPerFrame)
                {
                    if (index < endIndex)
                        SetIndex(index + 1);
                    else SetIndex(beginIndex);
                    nextFrameDelay -= secondPerFrame;
                }
            }
        }
    }
}


